<template>
  <div style="height: 100%">
    <el-container class="home-container">
      <el-header>
        <div>
          <img
            src="../assets/heima.png"
            alt=""
          >
          <span>电商平台管理后台</span>
        </div>
        <el-button
          type="info"
          @click="logout"
        >
          退出
        </el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div
            class="toggle-button"
            @click="toggleCollapse"
          >
            |||
          </div>
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            router
            :collapse="isCollapse"
            :collapse-transition="false"
          >
            <el-submenu
              v-for="item in menulist"
              :key="item.id"
              :index="item.id + ''"
            >
              <template slot="title">
                <!-- <i :class="iconsObj[item.id]" /> -->
                <i class="el-icon-search" />
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                v-for="subItem in item.children"
                :key="subItem.id"
                :index="'/' + subItem.path"
                @click="saveNavState('/' + subItem.path)"
              >
                <template>
                  <i class="el-icon-menu" />
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      menulist: [
        {
          id: 125,
          authName: '用户管理',
          path: 'user',
          children: [
            { id: 110, authName: '用户列表', path: 'users', children: [] }
          ]
        },
        {
          id: 103,
          authName: '角色管理',
          path: 'role',
          children: [
            { id: 111, authName: '角色列表', path: 'roles', children: [] },
            { id: 112, authName: '权限列表', path: 'rights', children: [] }
          ]
        }
      ],
      iconsObj: {
        125: 'iconfont',
        103: 'iconfont',
        101: 'iconfont',
        102: 'iconfont',
        145: 'iconfont'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return console.log('get menu fail')
      }
      this.menulist = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
